package com.example.text0926;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import java.util.Map;

public class adapter extends RecyclerView.Adapter<adapter.MyViewHolder> {

//    private List<String> list;
    private List<Map<String,Object>>list1;
    private Context context;
    private  View inflater;

//    public adapter(List<String> list){
//        this.list=list;
//    }

    public adapter(Context context, List<Map<String,Object>>
            list) {
        this.context = context;
        this.list1 = list;
    }

    @Override
    public adapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
//        Context context = viewGroup.getContext();
        // 正确地使用 LayoutInflater 来填充布局，并将返回的视图赋值给 view 变量
        inflater= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, viewGroup, false);
        // 使用填充好的视图来创建 MyViewHolder
        MyViewHolder myviewHolder = new MyViewHolder(inflater);
        return myviewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, int position) {
        Map<String,Object> currentItem=list1.get(position);
//        myViewHolder.imageView.setImageResource(Integer.parseInt(list1.get(position).get("name").toString()));
        myViewHolder.imageView.setImageResource((Integer)currentItem.get("phonename"));
//        myViewHolder.textView1.setText(list1.get(position).get("name").toString());
        myViewHolder.textView2.setText(list1.get(position).get("price").toString());
        myViewHolder.textView3.setText(list1.get(position).get("config").toString());

        myViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, Activity_contact_detail.class);
                intent.putExtra("price",list1.get(position).get("price").toString());
                intent.putExtra("config",list1.get(position).get("config").toString());
                context.startActivity(intent);
            }
        });
    }

    @Override
    /*RecyclerView的总共的行 ep.总共50个商品，每次展示10行，则ItemCount=50*/
    public int getItemCount() {return list1.size();}

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textView1,textView2,textView3;
        ImageView imageView;

        public MyViewHolder(View itemView) {/*构造方法*/
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView1);
//            textView1=itemView.findViewById(R.id.rc_textview1);
            textView2=itemView.findViewById(R.id.rc_textview2);
            textView3=itemView.findViewById(R.id.rc_textview3);
        }
    }
}
